package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.app.pojo.*;
import com.app.service.AppInfoService;
import com.app.service.CategoryService;
import com.app.service.DateDictionaryService;
import com.app.utill.PageBean;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Appinfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dev/flatform/app/")
public class AppinfoController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private DateDictionaryService dateDictionaryService;
    @Resource
    private CategoryService categoryService;
    @RequestMapping("/categorylevellist/{parentId}")
    @ResponseBody
    public String categorylevellist(@PathVariable Integer parentId){
        if(parentId == 0){
            parentId =null;
        }
        List<AppCategory> categoryList = categoryService.getCategoryList(parentId);
        return JSON.toJSONString(categoryList);
    }
    //分页查询,还有一级分类
    @RequestMapping("list")
    public String appList(HttpServletRequest request, @ModelAttribute ParamVO paramVO){
        //当前页

        if(paramVO.getPageIndex()==null){
            paramVO.setPageIndex(1);
        }
        //每页显示的条数
        paramVO.setPageSize(5);
        //分页
        //查询app状态
       List<DataDictionary> statusList = dateDictionaryService.getDicDictionaryList("APP_STATUS");
        //所属平台
       List<DataDictionary> flatFormList = dateDictionaryService.getDicDictionaryList("APP_FLATFORM");
        //查询一级分类
       List<AppCategory> categoryLevel1List = categoryService.getCategoryList(null);
       //一级分类有值查二级依次类推
        if(paramVO.getQueryCategoryLevel1()!=null){
            List<AppCategory> categoryLevel2List = categoryService.getCategoryList(paramVO.getQueryCategoryLevel1());
            request.setAttribute("categoryLevel2List",categoryLevel2List);
        }
        if(paramVO.getQueryCategoryLevel2()!=null){
            List<AppCategory> categoryLevel3List = categoryService.getCategoryList(paramVO.getQueryCategoryLevel2());
            request.setAttribute("categoryLevel3List",categoryLevel3List);
        }
       //回显

       request.setAttribute("querySoftwareName",paramVO.getQuerySoftwareName());
        request.setAttribute("queryStatus",paramVO.getQueryStatus());
        request.setAttribute("queryFlatformId",paramVO.getQueryFlatformId());
        request.setAttribute("queryCategoryLevel1",paramVO.getQueryCategoryLevel1());
        request.setAttribute("queryCategoryLevel2",paramVO.getQueryCategoryLevel2());
        request.setAttribute("queryCategoryLevel3",paramVO.getQueryCategoryLevel3());
        request.setAttribute("categoryLevel1List",categoryLevel1List);
        //存储数据

        request.setAttribute("statusList",statusList);
        request.setAttribute("flatFormList",flatFormList);
        PageBean<AppInfo> pages = appInfoService.getAppList(paramVO);
        request.setAttribute("pages",pages);
        request.setAttribute("appInfoList",pages.getResult());
        return "developer/appinfolist";
    }
    //添加
    @RequestMapping("/appversionadd/{appinfoid}")
    public String add(HttpServletRequest request,@PathVariable Integer appinfoid){
        List<Appinfo> appversionList2 =  appInfoService.getAppversion2(appinfoid);
        request.setAttribute("appVersionList",appversionList2);
        return "developer/appversionadd";
    }
    @RequestMapping("/addversionsave")
    public String addversionsave(HttpServletRequest request, @ModelAttribute AppVersion appVersion, MultipartFile multipartFile){
        System.out.println(appVersion);
        String fileUploadFiles = "";
        String fileDingYiName = "";
        //判断是否上传的文件
        if (multipartFile!=null) {
            //文件上传准备工作
            //准备上传的路径
            //获取绝对路径
             fileUploadFiles = request.getContextPath()+""+ File.separator+"statics"+File.separator+"uploadFiles";
            //获取原文件名
              String fileName = multipartFile.getOriginalFilename();
              //获取文件大小
            if(multipartFile.getSize()>5000000){
                request.setAttribute("fileUploadError","文件大小超出限制");
                return "developer/appversionadd";
            }
            //判断后缀名是否符合
            String suffix = fileName.substring(fileName.indexOf(".")+1);
            String prefix = fileName.substring(0,fileName.indexOf("."));
            //判断文件格式
            List<String> list = new ArrayList<String>();
            list.add("jpg");
            list.add("jpeg");
            list.add("png");
            //判断是否有以上后缀名
            if(!list.contains(suffix)){
                request.setAttribute("fileUploadError","文件格式不支持");
                return "developer/appversionadd";
            }
            //定义上传文件名
            fileDingYiName = prefix+(int)(Math.random()*(100000-10000)+10000)+""+System.currentTimeMillis()+"."+suffix;
            File file = new File(fileUploadFiles,fileDingYiName);
            if(!file.exists()){
                file.mkdirs();
            }
            try {
                multipartFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
                request.setAttribute("fileUploadError","系统错误,文件上传失败,请重试");
                return "developer/appversionadd";
            }

        }appVersion.setApkLocPath(File.separator+"statics"+File.separator+"uploadFiles"+File.separator+fileDingYiName);
      boolean result = appInfoService.addversionsave(appVersion);
      if (result){
          return "redirect:/dev/flatform/app/list";
      }else{
            return "developer/appversionadd";
      }
    }
    //跳转查看app信息
    @RequestMapping("/appview/{appinfoid}")
    public String appview(HttpServletRequest request, @PathVariable Integer appinfoid){
        AppInfo appInfo =  appInfoService.addview(appinfoid);
        List<Appinfo> appversionList =  appInfoService.getAppversion(appinfoid);
        request.setAttribute("appInfo",appInfo);
        request.setAttribute("appVersionList",appversionList);
        return "developer/appinfoview";
    }
    //添加
    @RequestMapping("/appinfoadd")
    public String addInfoAdd(){
        return "developer/appinfoadd";
    }
    //ajax加载所属平台
    @ResponseBody
    @RequestMapping("/datadictionarylist")
    public String dataDictionaryList(String tcode){
        List<DataDictionary> dicDictionaryList = dateDictionaryService.getDicDictionaryList(tcode);
        return JSON.toJSONString(dicDictionaryList);
    }



}
